Skip to content

fix: prevent allow notifications prompt on rotation #12201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

VougJo23
Copy link
Contributor

What is it?

  • [ x ] Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

Add variable permissionStatus to check if the user has already granted or denied permission.

Before/After Screenshots/Screen Record

  • Before:
notificationfix-before.mp4
  • After:
notificationfix-after.mp4

Fixes the following issue(s)

APK testing

The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR. You can find more info and a video demonstration on this wiki page.

Due diligence

@github-actions github-actions bot added the size/small PRs with less than 50 changed lines label Apr 23, 2025
@ShareASmile ShareASmile added bug Issue is related to a bug waiting for author If the author doesn't respond, the issue will be auto-closed. Otherwise the label will be removed. labels Apr 24, 2025
@Profpatsch
Copy link
Contributor

I posted some notes on the original issue:

#12194 (comment)

@Profpatsch
Copy link
Contributor

Profpatsch commented May 10, 2025

Okay here’s an answer by ChatGPT which I think corresponds exactly to what I figured out but in a pretty well-phrased manner:

To check whether a user has explicitly denied a permission in an Android app (especially if they selected "Don't ask again"), you can use a combination of the ActivityCompat.shouldShowRequestPermissionRationale() method and ContextCompat.checkSelfPermission().

Key Concepts:

  1. Permission denied temporarily: User just denied, but didn't select "Don't ask again".
  2. Permission denied permanently: User denied and selected "Don't ask again", or the system is configured to no longer prompt.

Code Example (Kotlin):

val permission = Manifest.permission.CAMERA

when {
    ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED -> {
        // Permission granted
    }

    ActivityCompat.shouldShowRequestPermissionRationale(activity, permission) -> {
        // Permission denied before, but "Don't ask again" NOT selected
        // Show rationale to user
    }

    else -> {
        // Permission denied before AND "Don't ask again" selected
        // Or first-time request on some devices
    }
}

Interpreting shouldShowRequestPermissionRationale():

  • Returns true: User denied the permission before, but did not select "Don't ask again".

  • Returns false:

    • The user denied and selected "Don't ask again", or
    • It's the first time asking (before any request was made).

To detect explicit denial with "Don't ask again":

You must track if the user was asked before, e.g., using SharedPreferences.

Combined Logic:

val sharedPref = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val wasPermissionRequestedBefore = sharedPref.getBoolean("camera_permission_requested", false)

if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
        // Previously denied, but not "Don't ask again"
    } else if (wasPermissionRequestedBefore) {
        // Denied with "Don't ask again"
    } else {
        // First time request
        sharedPref.edit().putBoolean("camera_permission_requested", true).apply()
    }
}

So I think the solution here is to save a single boolean as you said @Stypox, and then never ask again when the system dialog is fulfilled once (by setting it in the callback handler)

This seems to be the only reliable way of making sure we only ever
bother the user once. If they accept, from then on the permission
check will be successful. If they deny or swipe away, we never ask
again at startup and the user has to manually change it in the
settings or go to our settings and re-do the prompt by activating
notifications.
@github-actions github-actions bot added size/medium PRs with less than 250 changed lines and removed size/small PRs with less than 50 changed lines labels May 10, 2025
@Profpatsch
Copy link
Contributor

@Stypox I think I fixed it and implemented it correctly with the PermissionRequest callback thingy, PTAL

Copy link

@Profpatsch Profpatsch requested a review from Stypox May 10, 2025 12:36
@ShareASmile ShareASmile removed the waiting for author If the author doesn't respond, the issue will be auto-closed. Otherwise the label will be removed. label May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is related to a bug size/medium PRs with less than 250 changed lines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"allow NewPipe to send you notification" message appears again when changing from portait to landscape view
4 participants